using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using Habanero.Base.Exceptions;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestUIFormCreator
    {
        #region Setup/Teardown

        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [TearDown]
        public virtual void TearDownTest(){}
        #endregion

        [Test]
        public void Test_CreateField_WhenPropTypeNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            property.PropertyName = TestUtilsShared.GetRandomString();
            UIFormCreator boEditorCreator = new UIFormCreator();
            property.PropertyType = null;
            //---------------Assert Precondition----------------
            Assert.IsNull(property.PropertyType);
            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIField(property);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroApplicationException ex)
            {
                StringAssert.Contains("The property Type for", ex.Message);
                StringAssert.Contains("cannot be null in method 'UIFormCreator.GetControlType'", ex.Message);
            }
        }
        [Test]
        public void Test_CreateField_WhenPropClassNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNull(property.Class);
            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIField(property);
                Assert.Fail("expected HabaneroArgumentException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("You cannot create a formField for a property that is not associated with a class", ex.Message);
            }
        }
        [Test]
        public void Test_CreateField_WhenClassSolutionNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(property);
            property.Class = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNull(property.Class.Solution);
            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIField(property);
                Assert.Fail("expected HabaneroArgumentException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("You cannot create a formField for a property that is not associated with a Solution", ex.Message);
            }
        }

        [Test]
        public void Test_CreateField_ShouldSetPropertiesRelationship()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropStringType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.IsNotNull(formField);
            Assert.IsNotNull(formField.PropertyName);
            Assert.AreSame(property, formField.Property);
        }
        [Test]
        public void Test_CreateField_WhenString_ShouldHaveTextBoxPropertyAndControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            property.OrdinalPosition = 3;
            TestUtilsDMProperty.SetDMPropStringType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsString(property);
            Assert.IsNotNull(property.OrdinalPosition);
            Assert.Greater(property.OrdinalPosition, 0);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.IsNotNull(formField);
            Assert.IsNotNull(formField.PropertyName);
            Assert.AreEqual(property.PropertyName, formField.PropertyName);

            UICreatorTestUtils.AssertFieldControlTypeIsTextBox(formField.UIControlType);
            TestUtilsShared.AssertStringEmpty(formField.LabelText, "formField.LabelText", "The LabelText should not be set since it is defaulted to the Property DisplayValue");
            Assert.IsTrue(formField.Editable.GetValueOrDefault());
            Assert.IsFalse(formField.ShowAsCompulsory.GetValueOrDefault(false));
            Assert.IsFalse(formField.Generated.GetValueOrDefault());
            TestUtilsShared.AssertStringEmpty(formField.ToolTipText, "formField.ToolTipText", "The tool tip text should not be set it will default to the Property Description");
            Assert.AreEqual(property.OrdinalPosition, formField.OrdinalPosition);
        }
        [Test]
        public void Test_CreateField_WhenInt_ShouldHaveTextBoxType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropIntType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsInt(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.IsNotNull(formField);
            Assert.IsNotNull(formField.PropertyName);
            Assert.AreEqual(property.PropertyName, formField.PropertyName);

            UICreatorTestUtils.AssertFieldControlTypeIsTextBox(formField.UIControlType);
        }

        [Test]
        public void Test_CreateTwoFormFieldDefs_String_ShouldBothHaveSameControlType()
        {
            //---------------Set up test pack-------------------
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass();
            TestUtilsDMProperty.SetDMPropStringType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsString(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            UIField formField2 = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.AreNotSame(formField, formField2);
            Assert.AreSame(formField.UIControlType, formField2.UIControlType);
        }

        [Test]
        public void Test_CreateTwoFormFieldDefs_Boolean_ShouldBothHaveSameControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropBooleanType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsBool(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            UIField formField2 = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.AreNotSame(formField, formField2);
            Assert.AreSame(formField.UIControlType, formField2.UIControlType);
            UICreatorTestUtils.AssertControlTypeIsCheckBox(formField.UIControlType);
        }

        [Test]
        public void Test_CreateField_Bool_ShouldBeCheckBoxControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropBooleanType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsBool(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.AreEqual(property.PropertyName, formField.PropertyName);
            UICreatorTestUtils.AssertControlTypeIsCheckBox(formField.UIControlType);
        }

        [Test]
        public void Test_CreateField_Lookup_ShouldBeComboBoxControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropWithLookupList(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsLookup(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            UICreatorTestUtils.AssertControlTypeIsComboBox(formField, formField.UIControlType);
        }

        [Test]
        public void Test_CreateTwoFormFieldDefs_Lookup_ShouldBothHaveSameControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropWithLookupList(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsLookup(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            UIField formField2 = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            Assert.AreNotSame(formField, formField2);
            Assert.AreSame(formField.UIControlType, formField2.UIControlType);
        }

        [Test]
        public void Test_CreateField_DateTime_ShouldBeDateTimeControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            TestUtilsDMProperty.SetDMPropWithDateTime(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsDateTime(property);
            //---------------Execute Test ----------------------
            UIField formField = boEditorCreator.CreateUIField(property);
            //---------------Test Result -----------------------
            UICreatorTestUtils.AssertControlTypeIsDateTimePicker(formField.UIControlType);
        }

        [Test]
        public void Test_CreateUIForm_OneProperty_ShouldHaveAppropriateControlInfo()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(1, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(1, boEditorPanelInfo.UITabs.Count);
            UITab tab = boEditorPanelInfo.UITabs[0];
            Assert.AreEqual(1, tab.UIColumnLayouts.Count);
            UIColumnLayout layout = tab.UIColumnLayouts[0];
            Assert.AreEqual(1, layout.UIFields.Count);
            UIField formField = layout.UIFields[0];
            UICreatorTestUtils.AssertControlTypeIsComboBox(formField, formField.UIControlType);
            Assert.IsFalse(boEditorPanelInfo.Generated.GetValueOrDefault());
            Assert.AreSame(layout ,formField.UIColumnLayout);
            Assert.AreSame(tab, layout.UITab);
            Assert.AreSame(boEditorPanelInfo, layout.UITab.UIFormInfo);
            Assert.AreSame(boEditorPanelInfo, tab.UIFormInfo);
            Assert.IsTrue(formField.Status.IsValid(), formField.Status.IsValidMessage);
            Assert.IsTrue(layout.Status.IsValid(), layout.Status.IsValidMessage);
            Assert.IsTrue(tab.Status.IsValid(), tab.Status.IsValidMessage);
        }

        [Test]
        public void Test_CreateUIForm_TwoProperty_ShouldHaveAppropriateControlInfo()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(2, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            ReadOnlyCollection<UIField> fields = boEditorPanelInfo.UIFields;
            Assert.AreEqual(2, fields.Count);
            UIField formField1 = fields[0];
            UICreatorTestUtils.AssertControlTypeIsComboBox(formField1, formField1.UIControlType);
            UIField formField = fields[1];
            UICreatorTestUtils.AssertControlTypeIsCheckBox(formField.UIControlType);
        }

        [Test]
        public void Test_CreateUIForm_TwoProperty_OneObjectID_ShouldHaveObjectIDControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropStringType(property);
            dmClass.CreateObjectIdentity(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(3, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(3, boEditorPanelInfo.UIFields.Count);
        }

        [Test]
        public void Test_CreateUIForm_TwoProperty_OneKeepValuePrivate_ShouldHaveKeepvaluePrivateControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            property.KeepValuePrivate = true;
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(3, dmClass.Properties.Count);
            Assert.IsTrue(property.KeepValuePrivate.GetValueOrDefault());
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(3, boEditorPanelInfo.UIFields.Count);
            //TODO Brett 02 Apr 2009: Make sure that the Keep Value private is a password TextBox.
        }

        [Test]
        public void Test_CreateUIForm_WhenDMClassNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            UIFormCreator boEditorCreator = new UIFormCreator();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIForm(null);
                Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dmClass", ex.ParamName);
            }
        }

        #region CreateUIForm With SelectedProps

        [Test]
        public void Test_CreateUIForm_WithSelectedProps_OneProperty_ShouldHaveAppropriateControlInfo()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = dmClass.Properties.Select(prop => prop.PropertyName).ToList();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(1, props.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass, props);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(1, boEditorPanelInfo.UITabs.Count);
            UITab tab = boEditorPanelInfo.UITabs[0];
            Assert.AreEqual(1, tab.UIColumnLayouts.Count);
            UIColumnLayout layout = tab.UIColumnLayouts[0];
            Assert.AreEqual(1, layout.UIFields.Count);
            UIField formField = layout.UIFields[0];
            UICreatorTestUtils.AssertControlTypeIsComboBox(formField, formField.UIControlType);
            Assert.IsFalse(boEditorPanelInfo.Generated.GetValueOrDefault());
            Assert.AreSame(layout, formField.UIColumnLayout);
            Assert.AreSame(tab, layout.UITab);
            Assert.AreSame(boEditorPanelInfo, layout.UITab.UIFormInfo);
            Assert.AreSame(boEditorPanelInfo, tab.UIFormInfo);
            Assert.IsTrue(formField.Status.IsValid(), formField.Status.IsValidMessage);
            Assert.IsTrue(layout.Status.IsValid(), layout.Status.IsValidMessage);
            Assert.IsTrue(tab.Status.IsValid(), tab.Status.IsValidMessage);
        }

        [Test]
        public void Test_CreateUIForm_WithSelectedProps_TwoProperty_ShouldHaveAppropriateControlInfo()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = dmClass.Properties.Select(prop => prop.PropertyName).ToList();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(2, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass, props);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            ReadOnlyCollection<UIField> fields = boEditorPanelInfo.UIFields;
            Assert.AreEqual(2, fields.Count);
            UIField formField1 = fields[0];
            UICreatorTestUtils.AssertControlTypeIsComboBox(formField1, formField1.UIControlType);
            UIField formField = fields[1];
            UICreatorTestUtils.AssertControlTypeIsCheckBox(formField.UIControlType);
        }

        [Test]
        public void Test_CreateUIForm_WithSelectedProps_TwoProperty_OneObjectID_ShouldHaveObjectIDControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropStringType(property);
            dmClass.CreateObjectIdentity(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = dmClass.Properties.Select(prop => prop.PropertyName).ToList();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(3, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass, props);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(3, boEditorPanelInfo.UIFields.Count);
        }

        [Test]
        public void Test_CreateUIForm_WithSelectedProps_TwoProperty_OneKeepValuePrivate_ShouldHaveKeepvaluePrivateControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = UICreatorTestUtils.CreateClassWithOneLookupProperty();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            TestUtilsDMProperty.SetDMPropBooleanType(dmProperty);
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            property.KeepValuePrivate = true;
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = dmClass.Properties.Select(prop => prop.PropertyName).ToList();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(3, dmClass.Properties.Count);
            Assert.IsTrue(property.KeepValuePrivate.GetValueOrDefault());
            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass, props);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boEditorPanelInfo);
            Assert.AreEqual(3, boEditorPanelInfo.UIFields.Count);
            //TODO Brett 02 Apr 2009: Make sure that the Keep Value private is a password TextBox.
        }

        [Test]
        public void Test_CreateUIForm_WithSelectedProps_WhenDMClassNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = new List<string>();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIForm(null, props);
                Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dmClass", ex.ParamName);
            }
        }
        [Test]
        public void Test_CreateUIForm_WhenSelectedPropsNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = new List<string>();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIForm(new DMClass(), null);
                Assert.Fail("expected ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("selectedFormProps", ex.ParamName);
            }
        }

        [Test]
        public void Test_CreateUIForm_WhenSeletedPropIsNotAProp_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = new DMClass();
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            TestUtilsDMProperty.SetDMPropStringType(property);
            UIFormCreator boEditorCreator = new UIFormCreator();
            IList<string> props = new List<string>{"InvalidPropName"};
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.AreEqual(1, props.Count);
            //---------------Execute Test ----------------------
            try
            {
                boEditorCreator.CreateUIForm(dmClass, props);
                Assert.Fail("Expected to throw an ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
            }
        }
        #endregion



        [Test]
        public void Test_PropertyInSingleRelationship_AddedToFormFieldsAsRelationship()
        {
            //---------------Set up test pack-------------------
            UIFormCreator boEditorCreator = new UIFormCreator();
            const Cardinality cardinality = Cardinality.Single;

            DMRelationship relationship;
            DMClass dmClass = CreateDMClassWithRelationship(out relationship, cardinality);

            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);

            //---------------Test Result -----------------------
            Assert.AreEqual(dmClass.Properties.Count - 1 + dmClass.OwnerRelationships.Count, boEditorPanelInfo.UIFields.Count);

            UIField field = boEditorPanelInfo.UIFields[1];
            Assert.AreSame(field.PropertyName, relationship.RelationshipName );
            Assert.AreEqual(BOBroker.GetMapperAssemblyName(), field.UIControlMapperType.AssemblyName);
            Assert.AreEqual("AutoLoadingRelationshipComboBoxMapper", field.UIControlMapperType.TypeName);
            Assert.AreEqual("System.Windows.Forms", field.UIControlType.AssemblyName);
            Assert.AreEqual("ComboBox", field.UIControlType.TypeName);


            //---------------Tear Down -------------------------          
        }

        private DMClass CreateDMClassWithRelationship(out DMRelationship relationship, Cardinality cardinality) {
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClass relatedClass = TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);
            
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            property.PropertyName = TestUtilsShared.GetRandomString();
            property.PropertyType = TestUtilsLogic.GetGuidPropertyType();
            property.DefaultPropRule = null;
            relationship = dmClass.OwnerRelationships.CreateBusinessObject();

            relationship.Cardinality = cardinality;
            relationship.RelationshipName = TestUtilsShared.GetRandomString();
            DMRelationshipProperty relationshipProperty = relationship.RelationshipProperties.CreateBusinessObject();
            relationshipProperty.OwnerProperty = property;
            relationshipProperty.RelatedProperty = relatedClass.Properties[0];
            return dmClass;
        }

        [Test]
        public void Test_PropertyInMultipleRelationship_AddedToForm()
        {
            //---------------Set up test pack-------------------
            UIFormCreator boEditorCreator = new UIFormCreator();
            Cardinality cardinality = Cardinality.Multiple;

            DMRelationship relationship;
            DMClass dmClass = CreateDMClassWithRelationship(out relationship, cardinality);

            //---------------Execute Test ----------------------
            UIFormInfo boEditorPanelInfo = boEditorCreator.CreateUIForm(dmClass);

            //---------------Test Result -----------------------
            Assert.AreEqual(dmClass.Properties.Count, boEditorPanelInfo.UIFields.Count);

        }
    }

}